<?php
/**
 * 订单处理配置文件
 * Created by PhpStorm.
 * Author: 张志祥
 * QQ: 786811688
 * Phone: 13306281319
 * Date: 2017/6/21
 * Time: 15:49
 */

// 服务器环境、数据库配置、WEB回调URL
$config = array(
    'localhost.localdomain' => array( // 本地虚拟机
        'DB_HOST' => 'localhost',
        'DB_NAME' => 'caipiao',
        'DB_USER' => 'root',
        'DB_PWD' => '123456',
        'DB_PORT' => 3306,
        'REDIS_SERVER' => '127.0.0.1', //redis服务器IP
        'REDIS_PORT' => '6379', // redis服务端口
        'REDIS_PWD' => '', // redis密码
        'REDIS_PREFIX' => 'cpiao_', // redis前缀
    ),
    'local.d' => array( // 本地虚拟机
        'DB_HOST' => 'localhost',
        'DB_NAME' => 'caipiao',
        'DB_USER' => 'root',
        'DB_PWD' => 'Zzxx1688..',
        'DB_PORT' => 3306,
        'REDIS_SERVER' => '127.0.0.1', //redis服务器IP
        'REDIS_PORT' => '6379', // redis服务端口
        'REDIS_PWD' => '', // redis密码
        'REDIS_PREFIX' => 'cpiao_', // redis前缀
    ),
    'Dev_Test' => array(// 开发测试
        'DB_HOST' => 'localhost',
        'DB_NAME' => 'caipiao',
        'DB_USER' => 'caipiao',
        'DB_PWD' => 'Cippiia19829_',
        'DB_PORT' => 53306,
        'REDIS_SERVER' => '127.0.0.1', //redis服务器IP
        'REDIS_PORT' => '6379', // redis服务端口
        'REDIS_PWD' => 'Devzzxx123..', // redis密码
        'REDIS_PREFIX' => 'cpiao_', // redis前缀
    ),
);

if (isset($_SERVER['HOSTNAME'])) { // linux
    $config = $config[$_SERVER['HOSTNAME']];
    $tmpfs = '/dev/shm';

} else { // macos
    $config = $config['local.d'];
    $tmpfs = '/tmp/shm';
}


/**
 * 配置web端参数
 * @param $redis 连接实例
 * @param null $conf 动态配置参数
 * @return array
 */
function web_conf($redis, $conf=null)
{
    global $gpstr, $gp, $config;

    $prev = $config['REDIS_PREFIX'] . 'conf_';
    if ($conf) {
        // 动态改变配置信息
        $cnf = array();
        foreach ($conf as $key => $val) {
            $k = substr($val, strlen($prev));
            $cnf[$k] = $val;
        }
        $conf = $cnf;
    } else {
        // 定义web端配置信息
        $conf = [
            's_server' => '服务器设置',
            'u_risk' => '风控设置',
            's_supervene' => '并发控制',
            'u_service_fee' => '返点分配',
            'u_trading_times' => '交易时间',
            'u_trading_rules' => '交易规则',
            'u_o_money' => '订单金额类型',
        ];
    }

    foreach ($conf as $key => $val) {
        $value = $redis->hGetAll($prev . $key);
        switch ($key) {
            case 's_server':
                # 服务器设置
                foreach ($value as $k => $v) {
                    if ($k == 'web_server' || $k == 'web_server_innet') {
                        if (!filter_var($v, FILTER_VALIDATE_IP)) {
                            return ['status' => 0, 'info' => "web配置{$conf[$k]}异常" . json_encode($value) . "\n"];
                        }
                    } else {
                        $t = explode(':', $v);
                        if (!filter_var($t[0], FILTER_VALIDATE_IP)) {
                            return ['status' => 0, 'info' => "web配置{$conf[$k]}异常" . json_encode($value) . "\n"];
                        }
                    }
                    $gpstr->set($k, ['val' => $v]);
                }
                break;

            case 'u_risk':
                # 风控设置
                $risk = $value['deposit'] * $value['risk_rate'] / 100; // 区域代理风险值
                $risk_e = $value['deposit_e'] * $value['risk_rate_e'] / 100; // 特别代理风险值
                $gp->set('risk', ['val' => $risk]);
                $gp->set('risk_e', ['val' => $risk_e]);
                $gp->set('urgent', ['val' => $value['urgent']]);
                break;

            case 's_supervene':
                # 并发控制
                foreach ($value as $k => $v) {
                    # code...
                    if (!is_numeric($v) || $v < 0) {
                        return ['status' => 0, 'info' => "web配置{$conf[$key]}{$k}异常" . $v . "\n"];
                    }
                }

                $sell_overtime = $value['sell_overtime'] ? $value['sell_overtime'] : 5; // 平仓超时多少秒，开始控制
                $is_debug = $value['is_debug'] ? $value['is_debug'] : 0; // 调试开关，非0为开启

                $gp->set('sell_overtime', array('val' => $sell_overtime)); // 并发控制，允许平仓超时多少秒
                $gp->set('is_debug', array('val' => $is_debug)); // 调试开关，非0为开启
                $gp->set('max_buy', array('val' => 100)); // 每秒最大下单数量的默认值，运行时被改写为：下单或平仓处理速度
                $gp->set('max_queue', array('val' => 2000)); // 最大队列数量的默认值，运行时被改写为：平仓速度*最大订单时间
                break;

            case 'u_service_fee':
                # 手续费返佣比例
                $service_fee_key = ['third', 'fourth', 'dev', 'u_first', 'u_second'];
                if (count($value) != count($service_fee_key)) {
                    return ['status' => 0, 'info' => "web配置{$conf[$key]}异常" . json_encode($value) . "\n"];
                }

                $all = 0;
                foreach ($value as $k => $v) {
                    # code...
                    if (!in_array($k, $service_fee_key) || !is_numeric($v) || $v < 0) {
                        return ['status' => 0, 'info' => "web配置{$conf[$key]}{$k}异常" . $v . "\n"];
                    }
                    $all += $v;
                }
                if ($all > 100) {
                    return ['status' => 0, 'info' => "web配置{$conf[$key]}错误，总数>100" . json_encode($value) . "\n"];
                }
                $gpstr->set($key, ['val' => json_encode($value)]);
                break;

            case 'u_trading_times':
                # 交易时间
                foreach ($value as $k => $v) {
                    switch ($k) {
                        case 'holiday':
                            # 休市的法定假日，用英文逗号隔开
                            if ($v) {
                                $holiday = explode(',', $v);
                                foreach ($holiday as $k1 => $v1) {
                                    # code...
                                    if (!strtotime($v1)) {
                                        return ['status' => 0, 'info' => "web配置{$conf[$key]}{$k}异常" . $v1 . "\n"];
                                    }
                                }
                            }
                            break;

                        case 'closed':
                            # 人工休市停盘
                            break;

                        case 'sat_closed':
                            # 周六休市
                            break;

                        case 'sun_closed':
                            # 周日休市
                            break;

                        default:
                            # 开盘、停盘时间
                            if (!strtotime($v)) {
                                return ['status' => 0, 'info' => "web配置{$conf[$key]}，开盘、停盘时间错误" . $v . "\n"];
                            }
                            break;
                    }
                    $gpstr->set($k, ['val' => $v]);
                }
                break;

            case 'u_trading_rules':
                # 交易规则
                foreach ($value as $k => $v) {
                    if (!is_numeric($v) || $v < 0) {
                        return ['status' => 0, 'info' => "web配置{$conf[$key]}{$k}异常" . $v . "\n"];
                    }
                    $gp->set($k, ['val' => $v]);
                }
                break;

            case 'u_o_money':
                # 订单金额类型
                foreach ($value as $k => $v) {
                    $i = explode('_', $v);
                    if (!is_numeric($i[0]) || !is_numeric($i[1]) || $i[0] < 0 || $i[1] < 0) {
                        return ['status' => 0, 'info' => "web配置{$conf[$key]}{$k}异常" . $v . "\n"];
                    }
                    $gpstr->set($k, ['val' => $v]);
                }
                break;

            default:
                # code...
                break;
        }

    }
    $redis->del($config['REDIS_PREFIX'] . 'conf_change');
    return ['status' => 1, 'info' => '设置成功！'];
}


